package icu.zqyhdm.blog.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import icu.zqyhdm.blog.vo.TagVo;
import icu.zqyhdm.helper.JwtHelper;
import icu.zqyhdm.result.Result;
import icu.zqyhdm.blog.entity.Tag;
import icu.zqyhdm.blog.service.TagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * @Description
 * @Author lifu
 * @Date 2021/6/22 21:56
 */
@Api(tags = "文章标签的接口")
@RestController
@RequestMapping(value = "/api/blog/tag")
public class TagController {
    @Autowired
    private TagService tagService;

    @ApiOperation(value = "查询所有文章标签，标签名去重，并根据标签对应的文章数量降序排")
    @GetMapping(value = "/list")
    public Result List(){
        List<TagVo> tagVoList = tagService.getAllTag();
        return Result.ok(tagVoList);
    }

    @ApiOperation(value = "查询当前用户的文章标签，并根据标签对应的文章数量降序排")
    @GetMapping(value = "/myList")
    public Result List(@RequestHeader("X-Token") String token){
        Integer userId = JwtHelper.getUserId(token);
        List<TagVo> tagVoList = tagService.getTagByUserId(userId);
        return Result.ok(tagVoList);
    }

    @ApiOperation(value = "查询某个用户的文章标签，并根据标签对应的文章数量降序排")
    @GetMapping(value = "/list/{userId}")
    public Result List(@PathVariable Integer userId){
        List<TagVo> tagList = tagService.getTagByUserId(userId);
        return Result.ok(tagList);
    }

    @ApiOperation(value = "查询标签")
    @GetMapping(value = "/show/{tagId}")
    public Result add(@PathVariable Integer tagId){
        TagVo tagVo = tagService.getTagById(tagId);
        return Result.ok(tagVo);
    }

    @ApiOperation(value = "增加标签")
    @PostMapping(value = "/add")
    public Result add(@RequestHeader("X-Token") String token, @RequestParam("tagName") String tagName){
        tagService.saveTag(token,tagName);
        return Result.ok();
    }

    @ApiOperation(value = "修改标签")
    @PutMapping(value = "/update")
    public Result update(@RequestHeader("X-Token") String token, @Valid @RequestBody Tag tag){
        tagService.updateTag(token,tag);
        return Result.ok();
    }

    @ApiOperation(value = "删除标签")
    @DeleteMapping(value = "/delete/{id}")
    public Result delete(@RequestHeader("X-Token") String token,@PathVariable("id") Integer tagId){
        tagService.deleteTag(token,tagId);
        return Result.ok();
    }
}
